package org.xinghai.sql.main.controller;

import org.springframework.web.bind.annotation.*;
import org.xinghai.sql.common.api.Result;
import org.xinghai.sql.main.generator.service.DatabaseTablesService;
import org.xinghai.sql.main.generator.service.TableColumnsService;
import org.xinghai.sql.main.generator.service.TableRelationshipsService;


@RestController
@RequestMapping("/database/delete")
public class Dele {

    private final DatabaseTablesService databaseTablesService;
    private final TableColumnsService tableColumnsService;
    private final TableRelationshipsService tableRelationshipsService;

    // 使用构造器注入服务
    public Dele(DatabaseTablesService databaseTablesService,
                TableColumnsService tableColumnsService,
                TableRelationshipsService tableRelationshipsService) {
        this.databaseTablesService = databaseTablesService;
        this.tableColumnsService = tableColumnsService;
        this.tableRelationshipsService = tableRelationshipsService;
    }

    @DeleteMapping ("/clearAll")
    public Result<String> clearAllData() {
        try {
            // 清除所有表关系数据
            tableRelationshipsService.remove(null);
            // 清除所有列数据
            tableColumnsService.remove(null);
            // 清除所有表数据
            databaseTablesService.remove(null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.success("数据库已清空");
    }
    @DeleteMapping("/clearTableDatabase")
    public Result<String> clearTableData(@RequestParam("databaseName") String databaseName) {
       //清除databaseName下的所有表列
        try {
            tableColumnsService.removeByDatabaseName(databaseName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.success("数据库已清空");
    }
}